package com.panshi.controller;

import com.panshi.ISearchApi;
import com.panshi.IssApi;
import com.panshi.api.UserPoth;
import com.panshi.api.api.ArticlePort;
import com.panshi.api.api.TypePort;
import com.panshi.api.bean.ArticleBean;
import com.panshi.api.bean.TypeBean;
import com.panshi.api.util.PageSizeVO;
import com.panshi.bean.*;
import com.panshi.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

@Slf4j
@Controller
public class IndexController {

    @Autowired
    private CommonUtil commonUtil;

    @Autowired
    private TypePort typePort;

    @Autowired
    private ArticlePort articlePort;

    @Autowired
    private UserPoth userPoth;

    @Autowired
    private IssApi issApi;


//    @Qualifier("")
    @Autowired
    private ISearchApi iSearchApi;

    @GetMapping(value = {"/main","/"})
    public String index(HttpServletRequest request, Model model,String list) throws Exception {
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");

        log.info("当前登录用户："+auto_login+"***/"+list);

        if(auto_login.length()>0){
            UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));//查询用户
            model.addAttribute("user",userQuery);
        }





        PageSizeVO pageSizeBean = new PageSizeVO();//文章
        pageSizeBean.setPageSize(5);
        List<ArticleBean> yb_article_vos=null;
        List<IssBean> allIss = null;
        PageModel pageModel=new PageModel();//问题
        pageModel.setPageSize(5);



        pageModel.setStartIndex(1);
        pageSizeBean.setStartIndex(1);


        if( list==null){//推荐
            pageModel.setAnswer(1);
            allIss=issApi.findAllIss(pageModel);
            yb_article_vos = articlePort.queryArticleInfo(pageSizeBean);
        }else if("10".equals(list)){//热门
            allIss=issApi.findAllIss2(pageModel);
            yb_article_vos=articlePort.queryHotArticleInfo(pageSizeBean);
        }else if("11".equals(list)){//最新
            allIss=issApi.findAllIss3(pageModel);
            yb_article_vos=articlePort.queryNewArticleInfo(pageSizeBean);
        }


        model.addAttribute("indexType",list);


        Collections.shuffle(yb_article_vos);
        Collections.shuffle(allIss);
        model.addAttribute("consultList",yb_article_vos);
        model.addAttribute("alliss",allIss);


        pageSizeBean.setStartIndex(1);
        pageSizeBean.setPageSize(4);

        model.addAttribute("twConsult",(List<ArticleBean>)articlePort.queryHotArticleInfo(pageSizeBean));//图文热搜
        pageSizeBean.setPageSize(5);
        model.addAttribute("newConsult",(List<ArticleBean>)articlePort.queryNewArticleInfo(pageSizeBean));//最新创作

        model.addAttribute("auto",auto_login);
        model.addAttribute("typeList",typePort.queryArticleInfoAll());//牙齿类型
        model.addAttribute("tjList",issApi.findHostIss(0,0));//推荐问题

        return "main";
    }

    @PostMapping("/index/append")
    @ResponseBody
    public Object indexAppend(ModelAndView modelAndView, int indexPage,Integer type) throws Exception {
        PageSizeVO pageSizeBean = new PageSizeVO();//文章
        pageSizeBean.setPageSize(5);
        List<ArticleBean> yb_article_vos=null;
        List<IssBean> allIss = null;
        PageModel pageModel=new PageModel();//问题
        pageModel.setPageSize(5);

        pageModel.setStartIndex(indexPage);
        pageSizeBean.setStartIndex(indexPage);


        if( type==null ||type==0){//推荐
            pageModel.setAnswer(1);
            allIss=issApi.findAllIss(pageModel);
            yb_article_vos = articlePort.queryArticleInfo(pageSizeBean);
        }else if(type==10){//热门
            allIss=issApi.findAllIss2(pageModel);
            yb_article_vos=articlePort.queryHotArticleInfo(pageSizeBean);
        }else if(type==11){//最新
            allIss=issApi.findAllIss3(pageModel);
            yb_article_vos=articlePort.queryNewArticleInfo(pageSizeBean);
        }

        Collections.shuffle(yb_article_vos);
        Collections.shuffle(allIss);

        if(allIss.size()==0 && yb_article_vos.size()==0){
            return "";
        }

        modelAndView.addObject("consultList",yb_article_vos);
        modelAndView.addObject("alliss",allIss);

        modelAndView.setViewName("main_append");
        return modelAndView;
    }


    @PostMapping("/index/search")
    public String search(PageUtil pageUtil,Model model,HttpServletRequest request){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        model.addAttribute("auto",auto_login);
        if(auto_login.length()>0){
            UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));//查询用户
            model.addAttribute("user",userQuery);
        }
        //------------------------------上面是顶部信息
        pageUtil.setStartIndex(1);
        pageUtil.setPageSize(10);
        System.out.println(pageUtil);
        PageUtil<List<SearchBean>> listPageUtil = iSearchApi.searchData(pageUtil);
        List<SearchBean> searchBeans = iSearchApi.hostSearch();

        int total = listPageUtil.getTotal();
        double d=total/pageUtil.getPageSize();



        //Collections.shuffle(listPageUtil);//乱序输出
        System.out.println("====================================================");
        System.out.println(listPageUtil);

        model.addAttribute("pu",listPageUtil);
        model.addAttribute("kw",pageUtil.getKw());
        model.addAttribute("host",searchBeans);

        return "search/search";
    }

    @PostMapping("/search/append")
    @ResponseBody
    public Object searchAppend(PageUtil pageUtil,ModelAndView modelAndView){
        System.out.println(pageUtil);
        pageUtil.setPageSize(10);
        PageUtil<List<SearchBean>> listPageUtil = iSearchApi.searchData(pageUtil);
        if(listPageUtil==null){
            return "";
        }
        modelAndView.addObject("pu",listPageUtil);
        modelAndView.setViewName("search/search_append");
        return modelAndView;
    }

}
